const db = wx.cloud.database()
let id = '',
content = '',
currType = 'cet4' // 初始值为cet4
const operateData = wx.getStorageSync('operate')

// pages/wordBook/wordBook.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        DataList: [],
        selectItem: ["cet4", "cet6"],
        w_num: 0,
        inputNum: '',
        pageIndex: 0,
        pageSize: 20 // 一页显示条数
    },

    switchChange(e) {
        var currentTarget = e.currentTarget;
        var tab = currentTarget.dataset.index;
        currType = this.data.selectItem[tab]
        console.log(currType)
        this.setData({
            w_num:tab
        })

        this.getDataList() // 切换单词本后重新渲染数据
    },

    async getDataList(inputNum) {
        let {
            w_num,
            pageIndex,
            pageSize,
            DataList
        } = this.data
        inputNum = parseInt(inputNum)
        console.log('收到的pageIndex：',pageIndex)
        let {result} = await wx.cloud.callFunction({
            name: 'getOperateWordList',
            data: {
                type: w_num < 1 ? 'cet4' : 'cet6',
                pageIndex: inputNum?inputNum:pageIndex, // skip
                pageSize: pageSize // limit
            }
        })
        console.log(result)
        result = result.filter(function (item) { if(!item.address){return item} });
        this.setData({
            pageIndex:pageIndex,
            DataList: result
        })
        // console.log(DataList,result)
    },

    async onLoad(options) {
        await wx.cloud.callFunction({
            name: 'getOperateWordList',
            data: {
                type: currType,
                pageIndex: this.data.pageIndex,
                pageSize: this.data.pageSize
            }
        }).then(res => {
            let Res = res.result
            console.log(Res)
            Res = Res.filter(function (item) { if(!item.address){return item}});
            this.setData({
                DataList: Res
            })
        })
    },

    transfer(e) {
        console.log('e', e)
        id = e.target.dataset.val;
        content = e.target.dataset.content
        console.log('id：', id,content)
        wx.navigateTo({
          url: '/pages/audio/upWordAudio/upWordAudio?id='+id + '&content=' + content + '&type=' + currType + "&uptype=" + 'firstUp',
        })
    },

    // 输入框内容
    submit(e) {
        // searchword = e.detail.value
        console.log(e)
        let {
            inputNum
        } = this.data
        inputNum = e.detail.value
        console.log('inputNum', inputNum)
        this.getDataList(inputNum)
    },

    // scrolltolower() { // 触底
    //     let {
    //         pageIndex, inputNum
    //     } = this.data
    //     this.setData({
    //         pageIndex: inputNum?++inputNum:++pageIndex
    //     }, () => {
    //         this.getDataList(pageIndex)
    //     })

    //     console.log('触底时的pageIndex：',pageIndex)
    // },

})